﻿using IOP.SgrA;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace VkSample115;

public class TransshapeAnimation : Animation
{
    private ITexture? Texture;
    private float ratioT;
    private int operationT = 1;
    private float max_ration = 0.2f;

    public override void Play(TimeSpan timeSpan)
    {
        ratioT += (float)timeSpan.TotalSeconds * 0.1f * operationT;
        if (ratioT > max_ration)
        {
            ratioT = max_ration;
            operationT = -operationT;
        }
        if (ratioT < -max_ration)
        {
            ratioT = -max_ration;
            operationT = -operationT;
        }
        Texture?.UpdateTextureMemoryData(ratioT);
    }

    public override void OnAttach(IRenderObject @object)
    {
        RenderObject = @object;
        Texture = RenderObject.GetComponents<ITexture>().Where(x => x.SetIndex == 0 && x.Binding == 0).FirstOrDefault();
    }

    public override void CloneToNewRender(IRenderObject newObj)
    {
        var f = new TransshapeAnimation();
        f.Name = Name;
        newObj.AddComponent(f);
        f.OnAttach(newObj);
    }
}
